#pragma once

#ifndef DLL_LINKED_LIST_H
#define DLL_LINKED_LIST_H
#endif

//#include "bbAddon.h"

using namespace std;

static int _Debug_COUNTLINKS	= 0;

/* Double Linked-List */


class TLink{
	friend class TList;
	TList* _list;
	TLink* _next;
	TLink* _prev;
public :
	string _data;
	string _key; // optionnal value for tmap to list
	TLink();
	~TLink();
	inline TList* List(){return _list;};
	int InsertAfter(TLink*After);
	int InsertBefore(TLink*After);
	int Swap(TLink*l);
	TLink* Next();
	TLink* Prev();
	void Set(const char* pValue){_data=pValue;};
	void Set(const char* pValue, const char* pKey){_data=pValue;_key=pKey;};
	const char* Value(){return _data.c_str();};
	const char* Key(){return _key.c_str();};
	friend TLink* NullLink();
	friend inline void Unlink(TLink* lnk);
	friend int SortListDESC(TList* list);
	friend int SortListASC(TList* list);
	int Compare(TLink*lnk){return _data.compare(lnk->_data);}
};

inline void Unlink(TLink* lnk);

class TList{
public :
	TList();
	~TList();
	TLink* _head;
	TLink* _tail;
	inline TLink* First(){return _head;};
	inline TLink* Last(){return _tail;};
	int _Count;
	TList*Copy();
	TLink*AddLast(const char* pValue);
	TLink*AddLast(const char* pValue, const char* pKey);
	TLink*AddFirst(const char* pValue);
	TLink*AddFirst(const char* pValue,const char* pKey);
	TLink*Find(const char* pValue);
	TLink*FindKey(const char* pKey);
	int Sort(int sens);
	friend int SortListDESC(TList* list);
	friend int SortListASC(TList* list);
};

int SortListDESC(TList* list);
int SortListASC(TList* list);

